package tournoi.servlet;

import java.io.IOException;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import tournoi.modele.Arbitre;
import tournoi.modele.Equipe;
import tournoi.modele.Rencontre;
import tournoi.modele.Tournoi;
import tournoi.stateless.Gestion;

@WebServlet("/index")
public class PublicServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PublicServlet() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String action = request.getParameter("action");
		HttpSession session = request.getSession(true);

		Gestion facade = (Gestion) session.getAttribute("mafacade");

		if (facade == null) {
			Context ctx;
			try {
				ctx = new InitialContext();
				facade = (Gestion) ctx
						.lookup("java:global/TournoiWeb/GestionBean!tournoi.stateless.GestionLocal");
			} catch (NamingException e) {
				e.printStackTrace();
			}
			session.setAttribute("mafacade", facade);
		}

		/* MENU PRINCIPAL */
		if (action == null) {
			request.getRequestDispatcher("/public/accueil.jsp").forward(
					request, response);
		}
		/* LISTES */
		else if (action.equals("listeTournois")) {
			List<Tournoi> lt = (List<Tournoi>) facade.recupererTournois();
			request.setAttribute("tournois", lt);
			request.getRequestDispatcher("/public/listeTournois.jsp").forward(
					request, response);

		} else if (action.equals("listeEquipes")) {
			List<Equipe> le = (List<Equipe>) facade.recupererEquipes();
			request.setAttribute("equipes", le);
			request.getRequestDispatcher("/public/listeEquipes.jsp").forward(
					request, response);

		} else if (action.equals("listeArbitres")) {
			List<Arbitre> la = (List<Arbitre>) facade.recupererArbitres();
			request.setAttribute("arbitres", la);
			request.getRequestDispatcher("/public/listeArbitres.jsp").forward(
					request, response);
		}
		/*
		 * =================================
		 * 
		 * VUE INDIVIDUELLE
		 * 
		 * =================================
		 */
		else if (action.equals("voirTournoi")) {
			int id = Integer.parseInt(request.getParameter("id"));
			Tournoi t = facade.recupererTournoi(id);
			request.setAttribute("tournoi", t);
			request.getRequestDispatcher("/public/voirTournoi.jsp").forward(
					request, response);
		} else if (action.equals("voirMatch")) {
			int idMatch = Integer.parseInt(request.getParameter("match"));
			/* récupérer le match */
			Rencontre r = facade.recupererMatch(idMatch);
			request.setAttribute("match", r);
			request.getRequestDispatcher("/public/voirMatch.jsp").forward(
					request, response);
		}
	}
}
